import Vue from 'vue'
import Vuex from 'vuex'
import {setCookie,getCookie} from '../until/until'

Vue.use(Vuex)

const store = new Vuex.Store({
	state:{
		username: '',
		token:'',
		sidebar:{
			opened: getCookie('sidebarStatus')
		}
	},
	
	actions:{
		login(ctx,username) {
			ctx.commit('login',username)
		},
		logout(ctx){
			ctx.commit('logout')
		},
		ToggleSideBar(ctx){
			 ctx.commit('ToggleSideBar')
		},
		token(ctx,token){
			ctx.commit('token',token)
		}
	},
	mutations: {
		login (state,username){
			state.username = username
		},
		logout (state) {
			state.username = "";
		},
		ToggleSideBar (state){
			console.log(state.sidebar.opened)
			if(state.sidebar.opened){
				setCookie('sidebarStatus',1)
			}else{
				setCookie('sidebarStatus',0)
			}
			state.sidebar.opened = !state.sidebar.opened
			
		},
		token(state,token){
			state.token = token
		}
	}
});

export default store;